package vn.vtsmart.vtcalendarsync;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class CalendarListAdapter extends BaseAdapter {

	private List<CalendarNameItem> mData;
	private Activity mActivity;
	private static LayoutInflater mInflater = null;

	private TextView tvCalName;
	private CheckBox cboCal;

	public CalendarListAdapter(Activity activity, List<CalendarNameItem> data) {
		this.mActivity = activity;
		this.mData = data;
		mInflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (mData.size() <= 0)
			return 0;
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View vi = convertView;
		final int pos = position;
		if (convertView == null) {
			vi = mInflater.inflate(R.layout.calendar_row_item, null);
			tvCalName = (TextView) vi.findViewById(R.id.tvCalName);
			cboCal = (CheckBox) vi.findViewById(R.id.cboCal);
			tvCalName.setTypeface(Utils.getFontFromAssets(mActivity,
					Utils.FONT_NAME));
		}

		tvCalName.setText(mData.get(position).getmName());

		cboCal.setChecked(mData.get(position).ismIsChecked());
		cboCal.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
				mData.get(pos).setmIsChecked(arg1);
				Log.d("DungNT", "Check " + arg1);
			}
		});

		return vi;

	}

}
